﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PublicClass
{
    public class ResData
    {
        public String Name { get; set; }
        public ResultBuffer ResultBuffer { get; set; }
        public ResData(ResultBuffer res, string strName = "")
        {
            Name = strName;
            ResultBuffer = res;
        }

        /// <summary>
        /// 常规数据:过滤集使用名称(直线,类名:AcDbLine C++,Line C#, LINE)
        /// </summary>
        /// <param name="ent"></param>
        public ResData(Entity ent)
        {
            if (null == ent)
                return;

            Name = ent.GetRXClass().DxfName; // DXFname // cad搜索时用的名称(这一块单独出一个章节来介绍)
            ResultBuffer = new ResultBuffer();
            ResultBuffer.Add(new TypedValue((int)DxfCode.Start, ent.GetRXClass().DxfName));
            ResultBuffer.Add(new TypedValue((int)DxfCode.SoftPointerId, ent.ObjectId));
            ResultBuffer.Add(new TypedValue((int)DxfCode.Subclass, ent.GetRXClass().MyParent.Name));
            ResultBuffer.Add(new TypedValue((int)DxfCode.LayerName, ent.Layer));
            ResultBuffer.Add(new TypedValue((int)DxfCode.Int32, ent.ColorIndex));
        }

        public override string ToString()
        {
            StringBuilder str = new StringBuilder();
            if (Name != null)
            {
                str.Append(Name);
            }
            if (str.Length > 0)
            {
                str.Append(System.Environment.NewLine);
            }
            str.Append(ResultBuffer.ToStringEx());

            return str.ToString(); // 返回类名
        }
    }

    // 类扩展方法
    public static class ExtenHelper
    {
        public static string ToStringEx(this ResultBuffer resData)
        {
            if (null == resData)
                return "";
            StringBuilder str = new StringBuilder();
            foreach (var item in resData)
            {
                if (str.Length > 0)
                {
                    str.Append(System.Environment.NewLine);
                }
                str.Append(item.ToStringEx());
                // str.Append(item.ToString());
            }

            return str.ToString();
        }

        public static string ToStringEx(this TypedValue valueData)
        {
            string strValue = valueData.Value.ToString(); // (1001,表名)
            if ((int)DxfCode.SoftPointerId <= valueData.TypeCode && valueData.TypeCode <= (int)DxfCode.HardOwnershipId)
            {
                string strInt = strValue.Substring(1, strValue.Length - 2);
                long nValue = Convert.ToInt64(strInt);
                strValue = nValue.ToString("x8");
            }

            return valueData.TypeCode.ToString() + ":" + strValue;
        }
    }

    /// <summary>
    /// 实体数据转换的类
    /// </summary>
    public class EntDataConvertData
    {
        private ObjectId ID = ObjectId.Null;

        private List<ResData> EntData = new List<ResData>();
        private List<ResData> EntXData = new List<ResData>();
        private List<ResData> EntRecordData = new List<ResData>();

        public EntDataConvertData(ObjectId obj)
        {
            ID = obj;
            InitData();
        }
        /// <summary>
        /// 扩展数据
        /// </summary>
        /// <returns></returns>
        public string XData()
        {
            return ListResDataToString(EntXData);
        }

        /// <summary>
        /// 扩展记录
        /// </summary>
        /// <returns></returns>
        public string RecordData()
        {
            return ListResDataToString(EntRecordData);
        }

        /// <summary>
        /// 常规数据
        /// </summary>
        /// <returns></returns>
        public string Data()
        {
            return ListResDataToString(EntData);
        }

        public static string ListResDataToString(List<ResData> lst)
        {
            if (null == lst)
            {
                return "";
            }

            StringBuilder str = new StringBuilder();
            foreach (var item in lst)
            {
                if (str.Length > 0)
                {
                    str.Append(System.Environment.NewLine); // 不同的操作系统返回值是不一样的.
                }
                str.Append(item.ToString());
            }

            return str.ToString();
        }

        public static Transaction StartTranst()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            if (null == doc)
                return null;

            Autodesk.AutoCAD.DatabaseServices.Database db = doc.Database;

            return db.TransactionManager.StartOpenCloseTransaction();
        }

        void InitData()
        {
            using (Transaction ts = StartTranst())
            {
                DBObject obj = ts.GetObject(ID, OpenMode.ForRead);
                if (null == obj)
                    return;
                EntData.Add(new ResData(obj as Entity));

                EntXData.Add(new ResData(obj.XData));
                

                ObjectId idEx = obj.ExtensionDictionary;
                if (!idEx.IsNull)
                {
                    // 扩展数据,回头代码放置到gitee(国内的github)上
                    DBDictionary objDic = ts.GetObject(idEx, OpenMode.ForRead) as DBDictionary;
                    if (null != objDic)
                    {
                        foreach (DBDictionaryEntry item in objDic)
                        {
                            Xrecord xd = ts.GetObject(item.Value, OpenMode.ForRead) as Xrecord;
                            if (null != xd)
                            {
                                EntRecordData.Add(new ResData(xd.Data, item.Key));
                            }
                        }
                    }
                }
            }
        }
    }
}
